using System;

namespace Glue.Internals
{
    internal class NullableRuntimeType
    {
        private readonly Type underlyingType;
        private readonly object defaultValue;

        public NullableRuntimeType(Type nullableType)
        {
            underlyingType = nullableType.GetGenericArguments()[0];
            defaultValue = GetUnderlyingType().CreateInstance();
        }

        public static bool IsNullable(Type type)
        {
            if (type.IsGenericType)
            {
                var typedefinition = type.GetGenericTypeDefinition();
                return typedefinition == typeof(Nullable<>);
            }
            return false;
        }

        public Type GetUnderlyingType()
        {
            return underlyingType;
        }

        public object GetDefaultValue()
        {
            return defaultValue;
        }
    }
}